package com.gxa.modules.service.propertyservice.Impl;

import com.gxa.common.R;
import com.gxa.modules.mapper.propertymapper.VisitorMapper;
import com.gxa.modules.pojo.Visitor;
import com.gxa.modules.service.propertyservice.VisitorRegisterService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


@Service
public class VisitorRegisterImplService implements VisitorRegisterService {

    @Resource
    private VisitorMapper visitorMapper;
    @Override
    public R selectAllVisitorRegisterForPage(int page, int size) {
        //分页
        int index=(page-1)*size;
        R r = new R();
        HashMap<Object, Object> map = new HashMap<>();
        List<Visitor> visitorRegisters = visitorMapper.selectAllVisitorRegisterForPage(index,size);
        map.put("data",visitorRegisters);
        int res = visitorMapper.selectAllVisitorRegisterForPagesum( page,size);
        map.put("count",res);
        if(res > 0)
        {
            r.setCode(200);
            r.setMsg("查询成功");
            r.setData(map);
        }
//        if (visitorRegisters != null){
//            r.setCode(200);
//            r.setMsg("查询成功！");
//            r.setData(visitorRegisters);
//        }
        return r;

    }

    @Override
    public R selectAllVisitorRegister(int page, int size, String visitorName, String visitType, String phone) {
        int index=(page-1)*size;
        R r = new R();
        HashMap<Object, Object> map = new HashMap<>();
        List<Visitor> visitorRegisters = visitorMapper.selectAllVisitorRegister(index,size,
                visitorName,visitType,phone);

        map.put("data",visitorRegisters);


        int res = visitorMapper.selectAllVisitorRegistersum( visitorName,visitType,phone);
        map.put("count",res);
        if(res > 0)
        {
            r.setCode(200);
            r.setMsg("查询成功");
            r.setData(map);
        }


        return r;

    }

    @Override
    public R insertVisitorRegister(Visitor visitor) {
        R r = new R();
        System.out.println(visitor);
        int res = visitorMapper.insertVisitorRegister(visitor);
        if(res > 0)
        {
            r.setCode(200);
            r.setMsg("新增成功");
        }
        return r;
    }


//    SqlConnection myConn = new SqlConnection("Data Source=.;Initial Catalog=stumanage;User ID=sa;Password=123");
//　　string strComm = " INSERT INTO stuInfo(stuid,stuimage) VALUES(107,@photoBinary )";//操作数据库语句根据需要修改
//　　SqlCommand myComm = new SqlCommand(strComm, myConn);
//　　myComm.Parameters.Add("@photoBinary", SqlDbType.Binary, photo.Length);
//　　myComm.Parameters["@photoBinary"].Value = photo;
//　　myConn.Open();
//　　if (myComm.ExecuteNonQuery() > 0)
//        　　{
//　　 this.Label1.Text = "ok";
//　　}
//　　myConn.Close();



    @Override
    public R deleteVisitorRegisterById(String  id) {
        R<Object> r = new R<>();

        if ( id ==null || "".equals(id) ){
            r.setCode(500);
            r.setMsg("请选择要删除的信息！");
            return r;
        }

        r.setCode(500);
        r.setMsg("删除失败！");

        if(visitorMapper.deleteVisitorRegisterById(id)>0){
            r.setCode(200);
            r.setMsg("删除成功！");
        }

        return r;
    }

    @Override
    public void deleteVisitorRegister(List<Integer> list) {
        visitorMapper.deleteVisitorRegister(list);
    }


    @Override
    public R selectDetails(int id) {
        R r = new R();
        List<Visitor> visitorRegisters = visitorMapper.selectDetails(id);
        if (visitorRegisters != null){
            r.setCode(200);
            r.setMsg("查询成功！");
            r.setData(visitorRegisters);
        }
        return r;

    }

    @Override
    public R selectAllVisitorRecordForPage(int page, int size) {
        //分页
        int index=(page-1)*size;
        R r = new R();
        HashMap<Object, Object> map = new HashMap<>();
        List<Visitor> visitorRegisters = visitorMapper.selectAllVisitorRecordForPage(index,size);
        map.put("data",visitorRegisters);
        int res = visitorMapper.selectAllVisitorRecordForPagesum( page,size);
        map.put("count",res);
        if(res > 0)
        {
            r.setCode(200);
            r.setMsg("查询成功");
            r.setData(map);
        }
        return r;
    }

    @Override
    public R selectAllVisitorRecord(int page, int size, String visitorName, String phone) {
        int index=(page-1)*size;
        R r = new R();
        HashMap<Object, Object> map = new HashMap<>();
        List<Visitor> visitorRegisters = visitorMapper.selectAllVisitorRecord(index,size,
                visitorName,phone);

        map.put("data",visitorRegisters);


        int res = visitorMapper.selectAllVisitorRecordsum( visitorName,phone);
        map.put("count",res);
        if(res > 0)
        {
            r.setCode(200);
            r.setMsg("查询成功");
            r.setData(map);
        }
        return r;
    }

    @Override
    public R deleteVisitorRecordById(String id) {
        R<Object> r = new R<>();

        if ( id ==null || "".equals(id) ){
            r.setCode(500);
            r.setMsg("请选择要删除的信息！");
            return r;
        }

        r.setCode(500);
        r.setMsg("删除失败！");

        if(visitorMapper.deleteVisitorRecordById(id)>0){
            r.setCode(200);
            r.setMsg("删除成功！");
        }

        return r;
    }



}
